# install.packages("tigris")
# install.packages("tidyterra")
library(dplyr); library(sf); library(terra); library(tidyterra); library(ggplot2)
# Get elevation
nj <- tigris::counties(state = "NJ", cb = TRUE, class = "sf") %>%
st_transform(4326)
nj_dem <- geodata::elevation_30s(
country = "USA", path = tempdir(), res = 0.5) %>% crop(nj) %>% mask(nj)
# Calculate hillshade
slope <- terrain(nj_dem, "slope", unit = "radians")
aspect <- terrain(nj_dem, "aspect", unit = "radians")
hill <- shade(slope, aspect, 10, 340)
ggplot() +
geom_spatraster(data = hill, show.legend = FALSE) +
# Note the scale, grey colors
scale_fill_gradientn(
colours = grey(0:100 / 100),
na.value = "transparent") +
labs(title = "Hillshade of New Jersey") +
theme_void() +
theme(plot.title = element_text(
hjust = 0.5, # Centered
face = "bold.italic", # font face
family = "sans", size = 24, # font family and size
color = "#cc0033")) # Rutgers red